VERSION 0.6

ARG DOCKERHUB_USER_SECRET=+secrets/DOCKERHUB_USER
ARG DOCKERHUB_TOKEN_SECRET=+secrets/DOCKERHUB_TOKEN
ARG DOCKERHUB_MIRROR
ARG DOCKERHUB_MIRROR_INSECURE=false
ARG DOCKERHUB_AUTH=true
FROM ../..+earthly-integration-test-base \
    --DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
    --DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
    --DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
    --DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
    --DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE

IMPORT .. AS tests

WORKDIR /test

test:
    COPY expected-*.yml .

    # Ensure earthly doesn't create overridden config files when they don't exist
    RUN earthly --config config.yml config global.disable_analytics true 2>&1 | grep 'failed to read from config.yml: open config.yml: no such file or directory'

    RUN touch config.yml

    # Adding various configs of different types
    RUN earthly --config config.yml config global.disable_analytics true
    RUN test "$(cat config.yml)" = "$(cat expected-1.yml)"

    RUN earthly --config config.yml config 'git."example.com".password' hunter2
    RUN test "$(cat config.yml)" = "$(cat expected-2.yml)"

    RUN earthly --config config.yml config global.buildkit_additional_args "['userns', '--host']"
    RUN test "$(cat config.yml)" = "$(cat expected-3.yml)"

    RUN earthly --config config.yml config global.conversion_parallelism 5
    RUN test "$(cat config.yml)" = "$(cat expected-4.yml)"

    # --delete should remove a config
    RUN earthly --config config.yml config global.conversion_parallelism --delete
    RUN test "$(cat config.yml)" = "$(cat expected-5.yml)"

    # --help and -h should succeed
    RUN earthly config global.conversion_parallelism --help
    RUN earthly config global.conversion_parallelism -h
    # Ensure configs haven't changed by running help
    RUN test "$(cat config.yml)" = "$(cat expected-5.yml)"

    # Edge cases
    RUN earthly --config config.yml config global.conversion_parallelism oops; test $? = 1
    RUN earthly --config config.yml config global.conversion_parallelism ""; test $? = 1
    RUN earthly --config config.yml config global.buildkit_image ""

    # test earthly runs when no default config is present
    RUN ! test -f /root/.earthly/config.yml
    DO +RUN_EARTHLY_ARGS --earthfile="hello.earth" --target="+hello" --output_contains="greetings"

    # test earthly can write to default config location
    RUN earthly config global.disable_analytics true
    RUN test "$(cat /root/.earthly/config.yml)" = "$(cat expected-1.yml)"

    # test earthly fails when explicitly set to use a different config that doesn't exist
    DO +RUN_EARTHLY_ARGS --extra_args="--config=this-does-not-exist.yml" --earthfile="hello.earth" --target="+hello" --should_fail="true" --output_contains="failed to read from this-does-not-exist.yml"

    # test earthly runs with new cache percentage setting
    RUN earthly config global.cache_size_pct 50
    DO +RUN_EARTHLY_ARGS --earthfile="hello.earth" --target="+hello" --output_contains="greetings"

    # test that it still runs alongside a size settings
    RUN earthly config global.cache_size_mb 100
    RUN test "$(cat /root/.earthly/config.yml)" = "$(cat expected-6.yml)"
    DO +RUN_EARTHLY_ARGS --earthfile="hello.earth" --target="+hello" --output_contains="greetings"

    RUN touch /tmp/config.yml
    RUN chmod 400 /tmp/config.yml

    # test permission-related errors
    RUN addgroup -S testgroup
    RUN adduser -S -G testgroup testuser

    # required to allow testuser to call +RUN_EARTHLY_ARGS
    RUN chmod 0777 /tmp/earthly-script

    # required to allow testuser to call +RUN_EARTHLY_ARGS

    USER testuser
    WORKDIR /home/testuser

    # test correct user location is used by default
    RUN earthly config global.disable_analytics true
    RUN test "$(cat /home/testuser/.earthly/config.yml)" = "$(cat /test/expected-1.yml)"

    # check permission error is correctly returned
    COPY hello.earth Earthfile
    RUN ! earthly --config=/tmp/config.yml --verbose +hello > output.txt 2>&1
    RUN cat output.txt | grep 'Error: read config: failed to read from /tmp/config.yml: open /tmp/config.yml: permission denied'

    # check permission error is correctly returned even when earthly attempts to read from the default location
    # to make this test work, a symbolic link is required (chown root:root /home/testuser/.earthly/config.yml doesnt work)
    RUN rm /home/testuser/.earthly/config.yml
    RUN ln -s /tmp/config.yml /home/testuser/.earthly/config.yml

    RUN ! earthly --verbose +hello > output.txt 2>&1
    RUN cat output.txt | grep 'Error: read config: failed to read from /home/testuser/.earthly/config.yml: open /home/testuser/.earthly/config.yml: permission denied'


RUN_EARTHLY_ARGS:
    COMMAND
    ARG earthfile
    ARG target="+all"
    ARG extra_args
    ARG exec_cmd
    ARG should_fail=false
    ARG output_contains
    DO tests+RUN_EARTHLY \
        --earthfile=$earthfile \
        --target=$target \
        --should_fail=$should_fail \
        --exec_cmd=$exec_cmd \
        --output_contains=$output_contains \
        --extra_args=$extra_args \
        --DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
        --DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
        --DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
        --DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
        --DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE
